/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication6.GUI;

import javaapplication6.CONTROLLER.MonteCarloSimulationController;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author mathieubakrim
 */
public class ArgumentSetFrame extends  JFrame{
    
    protected MonteCarloSimulationController controller;
    private String[] strJlb;
    private String [] strJtf;
    private char [] chrArg;
    protected int set;
    
    private JTextField [] tabJtf;
    private JLabel[] tabJlb;
    
    private JComboBox jcbArg = new JComboBox();
    
    private JPanel thePanel = new JPanel();
    private JPanel jpl = new JPanel();
    private JPanel actionPanel = new  JPanel();
    
    private JLabel arg = new JLabel("Select the set to modify");
    
    private JButton ok = new JButton("Save");
    private JButton cancel = new JButton("Cancel");
    
    public ArgumentSetFrame(String[] strJlb, String [] strJtf, char [] chrArg, MonteCarloSimulationController theController) throws HeadlessException, FileNotFoundException, IOException {      
        this.controller = theController;
        this.setTitle("Edit argument sets");
        this.setSize(650, 350);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setContentPane(thePanel);
        thePanel.setLayout(new BorderLayout());
        
        this.strJlb = strJlb;
        this.strJtf = strJtf;
        this. chrArg = chrArg;
        
        tabJtf = new JTextField[this.strJtf.length];
        tabJlb = new JLabel[this.strJlb.length];
                
        GridLayout gl = new GridLayout(10,2);
        gl.setHgap(5);
        gl.setVgap(5);
        
        jpl.setLayout(gl);
        
        for(int j=0; j<strJlb.length;j++)
            tabJlb[j] = new JLabel(strJlb[j]);
        
        for(int k=0; k<strJtf.length;k++)
            tabJtf[k] = new JTextField();
        
        for(int m = 0; m<chrArg.length; m++)
            jcbArg.addItem(chrArg[m]);

        controller.csvReader();
        
        jcbArg.addActionListener(new JcbListener());
        jcbArg.setSelectedIndex(0); // preload 1st set thanks to the listener :)
        
        jpl.add(arg);
        jpl.add(jcbArg);
        for(int i = 0; i<strJlb.length;i++){
            jpl.add(tabJlb[i]);
            jpl.add(tabJtf[i]);            
        }        
        
        actionPanel.add(ok);
        actionPanel.add(cancel);
        
        ok.addActionListener(new SaveListener());
        cancel.addActionListener(new CancelListener());
        
        thePanel.add(jpl, BorderLayout.CENTER);
        thePanel.add(actionPanel, BorderLayout.SOUTH);
        this.setVisible(true);
    
    }

        class JcbListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent ae) {
            set = Integer.parseInt(jcbArg.getSelectedItem().toString());
            set -=1;
            tabJtf[0].setText(String.valueOf(controller.getTheMonteCarloSimulationsPreset().get(set).getS()));
            tabJtf[1].setText(String.valueOf(controller.getTheMonteCarloSimulationsPreset().get(set).getX()));            
            tabJtf[2].setText(String.valueOf(controller.getTheMonteCarloSimulationsPreset().get(set).getT()));
            tabJtf[3].setText(String.valueOf(controller.getTheMonteCarloSimulationsPreset().get(set).getR()));           
            tabJtf[4].setText(String.valueOf(controller.getTheMonteCarloSimulationsPreset().get(set).getB()));
            tabJtf[5].setText(String.valueOf(controller.getTheMonteCarloSimulationsPreset().get(set).getV()));
            tabJtf[6].setText(String.valueOf(controller.getTheMonteCarloSimulationsPreset().get(set).getnSteps()));
            tabJtf[7].setText(String.valueOf(controller.getTheMonteCarloSimulationsPreset().get(set).getnSimulations()));
            
            
        }
        
    }
       
    class SaveListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent ae) {
            controller.updateData(tabJtf, set);
            try {
                controller.csvWriter();
            } catch (IOException ex) {
                Logger.getLogger(ArgumentSetFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
            setVisible(false);
            dispose();
        }
        
    }
    class CancelListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent ae) {
            setVisible(false);
            dispose();
        }
        
    }

}
